﻿using Microsoft.EntityFrameworkCore;
using Restful_Api.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Restful_Api.Data
{
    public class RoutineDbContext : DbContext
    {
        public RoutineDbContext(DbContextOptions<RoutineDbContext> options) : base(options)
        {

        }
        public DbSet<Company> Companies { get; set; }
        public DbSet<Employee> Employees { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Company>()
                .Property(x => x.Name).IsRequired().HasMaxLength(100);
            modelBuilder.Entity<Company>()
                .Property(x => x.Introduction).HasMaxLength(500);

            modelBuilder.Entity<Employee>()
                .Property(x => x.EmployeeNo).IsRequired().HasMaxLength(10);
            modelBuilder.Entity<Employee>()
                .Property(x => x.FistName).IsRequired().HasMaxLength(50);
            modelBuilder.Entity<Employee>()
                .Property(x => x.LastName).IsRequired().HasMaxLength(50);

            modelBuilder.Entity<Employee>()
                .HasOne(x => x.Company)
                .WithMany(x => x.Employees)
                .HasForeignKey(x => x.CompanyId)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<Company>().HasData(
                new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "微软公司",
                    Introduction = "微软的公司简介",
                    Country="美国",
                    Industry="计算机",
                    Product="微软的产品"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "Google",
                    Introduction = "谷歌的公司简介" ,
                    Country = "中国",
                    Industry = "浏览器",
                    Product = "谷歌的产品"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "BiliBili",
                    Introduction = "BiliBili的公司简介",
                     Country = "中国",
                    Industry = "直播",
                    Product = "bili的产品"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "华为",
                    Introduction = "HuaWei的公司简介",
                     Country = "中国",
                    Industry = "手机",
                    Product = "华为的产品"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "小米",
                    Introduction = "小米的公司简介"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "三星",
                    Introduction = "三星的公司简介"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "魅族",
                    Introduction = "魅族的公司简介"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "oppo",
                    Introduction = "oppo的公司简介"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "Vivo",
                    Introduction = "Vivo的公司简介"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "苹果",
                    Introduction = "苹果的公司简介"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "香蕉",
                    Introduction = "香蕉的公司简介"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "雪梨",
                    Introduction = "香蕉的公司简介"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "橙子",
                    Introduction = "橙子的公司简介"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "葡萄",
                    Introduction = "葡萄的公司简介"
                }, new Company()
                {
                    Id = Guid.NewGuid(),
                    Name = "桃子",
                    Introduction = "桃子的公司简介"
                }
                );
            modelBuilder.Entity<Employee>().HasData(
                new Employee()
                {
                    Id = Guid.NewGuid(),
                    CompanyId=Guid.Parse("7cad215a-131c-4d7b-9167-c9a755143425"),
                    FistName = "张",
                    LastName = "三",
                    DateOfBirth = new DateTime(2020, 8, 10),
                    EmployeeNo = "G003",
                    Gender = Gender.男
                }, new Employee()
                {
                    Id = Guid.NewGuid(),
                    CompanyId = Guid.Parse("7cad215a-131c-4d7b-9167-c9a755143425"),
                    FistName = "李",
                    LastName = "四",
                    DateOfBirth = new DateTime(2020, 8, 15),
                    EmployeeNo = "G003",
                    Gender = Gender.男
                },
                 new Employee()
                 {
                     Id = Guid.NewGuid(),
                     CompanyId = Guid.Parse("9e48c982-c52b-46d9-bc9c-d98da8fee770"),
                     FistName = "小",
                     LastName = "Lu",
                     DateOfBirth = new DateTime(2020, 8, 20),
                     EmployeeNo = "G003",
                     Gender = Gender.女
                 },
                 new Employee()
                 {
                     Id = Guid.NewGuid(),
                     CompanyId = Guid.Parse("ac83f843-9cd2-449e-a5f6-656229c617d5"),
                     FistName = "小",
                     LastName = "江",
                     DateOfBirth = new DateTime(2020, 8, 29),
                     EmployeeNo = "G003",
                     Gender = Gender.男
                 }
                );
        }
    }
}
